package lgq.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lgq.service.CourseManager;
import lgq.service.EmpManager;
import lgq.service.TeacherManager;
import lgq.service.TrainCourseManager;
import lgq.service.TrainManager;
import lgq.service.TrainPropertyManager;
import lgq.service.TrainTypeManager;
import lgq.service.TrainWayManager;
import model.Course;
import model.Teacher;
import model.Train;
import model.TrainCourse;

public class AddTrainServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {		
		String course[]=request.getParameterValues("course");
		String percent[]=request.getParameterValues("percent");
		String teacher[]=request.getParameterValues("teacher");
		Train train=new Train();
		train.setTrainname(request.getParameter("trainname"));
		train.setAddress(request.getParameter("address"));
		train.setMoney(Double.parseDouble(request.getParameter("money")));
		train.setOrganization(request.getParameter("organization"));
		TrainTypeManager ttm=new TrainTypeManager();		
		train.setTrainType(ttm.getById(Integer.parseInt(request.getParameter("traintype"))));
		TrainPropertyManager tpm=new TrainPropertyManager();
		train.setTrainProperty(tpm.getById(Integer.parseInt(request.getParameter("trainProperty"))));
		TrainWayManager twm=new TrainWayManager();
		train.setTrainWay(twm.getById(Integer.parseInt(request.getParameter("trainWay"))));
		EmpManager em=new EmpManager();
		train.setManager(em.getById(new TrainManager().getIDByEmpno(request.getParameter("manager"))));
		try {
			train.setStarttime(new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("starttime")));
			train.setEndtime(new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("endtime")));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		List<TrainCourse> list=new ArrayList<TrainCourse>();
		for(int i=0;i<course.length;i++)
		{
			TrainCourse tc=new TrainCourse();
			Course c=new Course();
			c=new CourseManager().getById(Integer.parseInt(course[i]));
			tc.setCourse(c);
			tc.setPercent(Double.parseDouble(percent[i]));
			Teacher t=new Teacher();
			t=new TeacherManager().getById(Integer.parseInt(teacher[i]));
			tc.setTeacher(t);
			list.add(tc);
			
		}
		train.setTrainCourse(list);
		TrainManager tm=new TrainManager();		
		tm.addTrain(train);
		request.getRequestDispatcher("/FindTrainServlet").forward(request,response);
	}

}
